package com.example.mywanandroid.project.business;

import com.example.mywanandroid.project.ProjectRecyclerViewItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ParseData {
    public static List<String> parseProjectTitleData(String rawData) {
        List<String> res = new ArrayList<>();
        try {
            JSONObject projectTitleData = new JSONObject(rawData);
            JSONArray bannerArray = projectTitleData.getJSONArray("data");
            for (int i = 0; i < bannerArray.length(); ++i) {
                String projectName = bannerArray.getJSONObject(i).getString("name");
                res.add(projectName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return res;
    }
    public static List<ProjectRecyclerViewItem> parseProjectRecyclerViewData(String rawData) {
        List<ProjectRecyclerViewItem> res = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(rawData);
            JSONArray array = object.getJSONObject("data")
                    .getJSONArray("datas");
            for(int i = 0; i < array.length(); ++i) {
                JSONObject object1 = array.getJSONObject(i);
                res.add(new ProjectRecyclerViewItem(
                        object1.getString("envelopePic"),
                        object1.getString("title"),
                        object1.getString("desc"),
                        object1.getString("niceShareDate"),
                        object1.getString("author"),
                        object1.getString("link")
                ));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return res;
    }
}
